<?php
/**
 * Hoffman Framework
 *
 * LICENSE
 *
 * This source file is subject to the GNU LGPL 2.1 license that is bundled
 * with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to annop@thaicyberpoint.com so we can send you a copy immediately.
 *
 * @package	   Hoffman
 * @author     Ford AntiTrust
 * @copyright  Copyright (c) 2008 Ford AntiTrust (http://www.thaicyberpoint.com/ford/blog)
 * @since	   Version Rv 0.2
 * @license    http://creativecommons.org/licenses/LGPL/2.1
 *             GNU LGPL 2.1
 * @filesource
 */

if (!defined('INDEX'))
    die('No direct script access allowed.');

/**
 * @see Hmf
 */
require_once 'Hmf.php';

/**
 * Pagination
 *
 * Slice your data for instance, threads on a page might be paginated such that N appear on each page.
 *
 * @package		Hoffman
 * @subpackage	Hmf
 * @category	Pagination
 * @author		Ford AntiTrust
 * @version     SVN: $Id: Pagination.php 150 2009-03-20 07:06:54Z annop.s@gmail.com $
 * @link		http://www.thaicyberpoint.com/ford/blog/hmf
 */

class Hmf_Pagination extends Hmf {

    private $page, $offset, $totalsRowsData, $totalPages, $viewPerPage, $txtPrevPage, $txtNextPage, $txtPagination;

    private $numShowPages = 3;

    private $paramQueryString = 'page';

    function __construct($page, $totalsRowsData, $viewPerPage = 25) {

        $this->setPage($page);
        $this->setTotalsRowsData($totalsRowsData);
        $this->setViewPerPage($viewPerPage);
    }

    function setOffSet($offset = 0) {

        $this->offset = $offset;
    }

    function getOffset() {

        return $this->offset;
    }

    function setPage($page = 1) {

        $this->page = $page;
    }

    function getPage() {

        return $this->page;
    }

    function setTotalsRowsData($totalsRowsData) {

        $this->totalsRowsData = $totalsRowsData;
    }

    function getTotalsRowsData() {

        return $this->totalsRowsData;
    }

    function setTotalPages($value) {

        $this->totalPages = $value;
    }

    function getTotalPages() {

        $this->setTotalPages(ceil($this->getTotalsRowsData() / $this->getViewPerPage()));
        return $this->totalPages;
    }

    function setViewPerPage($viewPerPage = 25) {

        $this->viewPerPage = $viewPerPage;
        if ($this->getPage() == 1 or empty($this->page)) {
            $this->setOffSet(0);
            $this->setPage(1);
        } else {
            $this->setOffSet($this->getViewPerPage() * ($this->getPage() - 1));
        }
    }

    function getViewPerPage() {

        return $this->viewPerPage;
    }

    function getNumShowPages() {

        return $this->numShowPages;
    }

    function setNumShowPages($num) {

        $this->numShowPages = $num;
    }

    function setParamQueryString($name = 'page') {

        $this->paramQueryString = $name;
    }

    function processLink($url, $prefixId = null, $numShowPages = null) {

        $totalPages = $this->getTotalPages();
        $page = $this->getPage();

        if (is_null($prefixId))
            $prefixId = 'hmf-pagination';

        if (!is_null($numShowPages))
            $this->setNumShowPages($numShowPages);

        $parsedUrl = parse_url($url);

        $strSubfixPage = (empty($parsedUrl['query']) ? '?' : '&') . $this->paramQueryString . '=';

        $firstPage = $url . $strSubfixPage . '1';
        $lastPage = $url . $strSubfixPage . ($totalPages);
        $nextPage = $url . $strSubfixPage . ($page + 1);
        $prevPage = $url . $strSubfixPage . ($page - 1);

        $this->txtPrevPage .= '<li><a class="prevnext'.($page > 1 ? '':' disablelink').'" href="' . ($page > 1 ? $prevPage :'#') . '" id="' . $prefixId . '-previous"> ' . $this->_t('« Previous') . '</a></li>'.PHP_EOL;

        // -----------
        if ($totalPages > 1) {

            if ($page > ($this->getNumShowPages() + 1)) {

                $this->txtPagination .=
                '<li><a href="' . $firstPage . '" id="' . $prefixId . '-first-page">1</a></li>' . PHP_EOL
                . '<li><a href="#">...</a></li>'.PHP_EOL;

            }

            for ($i = ($page - $this->getNumShowPages()); ($i < $page and $i <= $page + $this->getNumShowPages()); $i++) {

                if ($i > 0)
                    $this->txtPagination .= '<li><a href="' . $url . $strSubfixPage . $i . '" id="' . $prefixId . '-' . $i . '">' . $i . '</a></li>'.PHP_EOL;

            }

            $this->txtPagination .= ' <li><a class="currentpage" href="' . $url . $strSubfixPage . $page . '" id="' . $prefixId . '-' . $page . '">' . $page . '</a></li>'.PHP_EOL;

            for ($i = $page + 1; ($i <= $page + $this->getNumShowPages() and $i <= $totalPages); $i++) {

                $this->txtPagination .= '<li><a href="' . $url . $strSubfixPage . $i . '" id="' . $prefixId . '-' . $i . '">' . $i . '</a></li>'.PHP_EOL;

            }

            if ($page + $this->getNumShowPages() < $totalPages) {

                $this->txtPagination .= '<li><a href="#">...</a></li>'.PHP_EOL
                . '<li><a href="' . $lastPage . '" id="' . $prefixId . '-last-page">' . $totalPages . '</a></li>'.PHP_EOL;

            }

        } else {

            $this->txtPagination .= ' <li><a class="currentpage" href="' . $url . $strSubfixPage . $page . '" id="' . $prefixId . '-' . $page . '">' . $page . '</a></li>'.PHP_EOL;

        }
        // -----------

        $this->txtNextPage = '<li><a class="prevnext'.($page < $totalPages ? '':' disablelink').'" href="' . ($page < $totalPages ? $nextPage :'#') . '" id="' . $prefixId . '-next"> ' . $this->_t('Next »') . ' </a></li>'.PHP_EOL;

    }

    function getLink($url, $prefixId = null, $numShowPages = null) {

        if (is_null($prefixId))
            $prefixId = 'hmf-pagination';

        $this->processLink($url, $prefixId, $numShowPages);

        $txt_output = '<div class="pagination"><ul>' .PHP_EOL . $this->txtPrevPage .PHP_EOL . $this->txtPagination .PHP_EOL . $this->txtNextPage . PHP_EOL . '</ul></div>';

        return $txt_output;
    }

    /**
     * Get pagination link with array
     * @param $key [nav|prev|next]
     * @return string
     */
    function getLinkWithArray($key = null) {

        $output = array();
        $output['nav'] = $this->txtPagination;
        $output['prev'] = $this->txtPrevPage;
        $output['next'] = $this->txtNextPage;

        if (!is_null($key) and in_array($key, $output)) {
            return $output[$key];
        }
        return $output;
    }

}
?>